﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace cjwygl
{
    public partial class FrmRoleMgr : Form
    {
        public FrmRoleMgr()
        {
            InitializeComponent();
        }

        private void FrmRoleMgr_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“cjwyglDataSet_role.CJ_ROLE”中。您可以根据需要移动或删除它。
            LoadData();

        }
        private void LoadData()
        {
            this.cJ_ROLETableAdapter.Fill(this.cjwyglDataSet_role.CJ_ROLE);
        }
        private void button_add_Click(object sender, EventArgs e)
        {
            FrmRoleUpdate frmUpdate = new FrmRoleUpdate();
            frmUpdate.ShowDialog();
        }

        private void button_update_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("未选中任何行");
                return;
            }
            else
            {
                int id = -1;
                int.TryParse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), out id);
                FrmRoleUpdate frmupdate = new FrmRoleUpdate(id);
                frmupdate.ShowDialog();

            }
        }

        private void button_refresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void button_delete_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要删除吗？", "删除该条记录", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("未选中任何行");
                return;
            }
            else
            {
                if (dataGridView1.SelectedRows[0].Cells[1].Value.ToString() == userinfo.rolename)
                {
                    MessageBox.Show("登录状态下,不能删除自己所属的角色");
                    return;
                }
                int id = -1;
                int.TryParse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), out id);
                if (id == -1)
                {
                    MessageBox.Show("系统错误，未识别的行");
                }
                else
                {
                    using (DataBase db = new DataBase())
                    {
                        string sql = "delete from CJ_ROLE where ID=@ID";
                        db.AddParameter("ID", id);
                        int ret = db.ExecuteNonQuery(sql);
                        MessageBox.Show("已删除" + ret + "行");
                        LoadData();
                    }
                }

            }
        }
    }
}
